package com.xiaoyang.ssyx.acl.utils;

import com.xiaoyang.ssyx.model.acl.Permission;

import java.util.ArrayList;
import java.util.List;

/**
 * @AUTHOR XiaoYang
 * @DATE 2023/6/14-17:05
 * @DESCRIPTION TODO
 */
public class PermissionHelper {

    public static List<Permission> buildPermission(List<Permission> allList) {
        List<Permission> trees = new ArrayList<>();
        //遍历所有菜单List集合，得到第一层数据，pid=0
        for (Permission permission : allList) {
            if (permission.getPid() == 0){
                permission.setLevel(1);
                //调用方法，从第一层开始往下找
                trees.add(findChildren(permission,allList));
            }
        }
        return trees;
    }

    //递归往下找
    //permission上层节点，从这里往下找
    //allList所有菜单
    private static Permission findChildren(Permission permission, List<Permission> allList) {
        permission.setChildren(new ArrayList<>());
        //遍历allList所有菜单数据
        //1判断：当前节点id=pid是否一样，封装，递归往下找
        for (Permission it : allList) {
            if (permission.getId().longValue() == it.getPid().longValue()){
                int level = permission.getLevel() + 1;
                it.setLevel(level);
                if (permission.getChildren() == null){
                    permission.setChildren(new ArrayList<>());
                }
                //封装下一层数据
                permission.getChildren().add(findChildren(it,allList));
            }
        }
        return permission;
    }
}
